package es.xpt.ysf.commons.configuration;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import es.xpt.ysf.commons.configuration.model.ParameterValue;
import es.xpt.ysf.dao.Connection;
import es.xpt.ysf.dao.DAOErrors;
import es.xpt.ysf.dao.DAOException;
import es.xpt.ysf.dao.DbDAO;
import es.xpt.ysf.dao.PreparedStatement;
import es.xpt.ysf.jaas.YSFPrincipal;

/**
 * 
 * Clase con los accesos a datos para las observaciones.
 * 
 * @author nacho.estrada
 *
 */
public class ConfigurationDAO extends DbDAO<ParameterValue> {
	
	private static final String PARAMETER_FIELDS =" PAR.code PAR_code, " +
	" PAR.environment PAR_environment, PAR.modifyDate PAR_modifyDate," +
	" PAR.user PAR_user, PAR.value PAR_value" ;

	public ConfigurationDAO(Connection c, YSFPrincipal principal, DAOErrors errors) {
		super(c, principal, errors);
	}

	@Override
	protected ParameterValue populateBean(ResultSet rs) throws SQLException {
		
		ParameterValue par = new ParameterValue();	
		par.setCode(rs.getString("PAR_code"));
		par.setEnvironment(rs.getString("PAR_environment"));
		par.setModifyDate(rs.getDate("PAR_modifyDate"));
		par.setUser(rs.getString("PAR_user"));
		par.setValue(rs.getString("PAR_value"));
		return par;

	}


	public ParameterValue getParameter(String parameterName, 
			String environment) throws DAOException {
		String selectStatement = "select "+ PARAMETER_FIELDS + " from ysf_cc_parameterValue PAR"
			+" where PAR.code = '" + parameterName + "'  and PAR.environment = '" + environment + "' ";
			
			return singleQuery(selectStatement);
	}

	public void updateParameter(ParameterValue parameter) throws DAOException {
		String statement = " update ysf_cc_parameterValue set "
			+ " value  = ?, modifyDate  = ?, user = ? "
			+ " where  code = ? ";
				    
	    PreparedStatement stmt = null;
	    try {
	      stmt = con.prepareStatement(statement); 
	      stmt.setString(1, parameter.getValue());
	      stmt.setDate(2, parameter.getModifyDate());
	      stmt.setString(3, parameter.getUser());
	      stmt.setString(4, parameter.getCode());
	      stmt.executeUpdate();
	   
	    } catch (SQLException se){
	      throw new DAOException(se, errors);
	    } finally {
	      if (stmt != null ) stmt.close();
	    }
		
	}

	public void insertParameter(ParameterValue parameter) throws DAOException {
		String statement = "insert into ysf_cc_parameterValue "
		      + " (value, modifyDate, user,  code, environment)"
		      + " VALUES (?, ?, ?, ?, ?)";
	    PreparedStatement stmt = null;
	    try {
	      stmt = con.prepareStatement(statement);
	      stmt.setString(1, parameter.getValue());
	      stmt.setDate(2, parameter.getModifyDate());
	      stmt.setString(3, parameter.getUser());
	      stmt.setString(4, parameter.getCode());
	      stmt.setString(5, parameter.getEnvironment());
	   
	      stmt.executeUpdate();
	      
	    } catch (SQLException se){
	      throw new DAOException(se, errors);
	    } finally {
	      if (stmt != null) stmt.close();
	    }
		
	}

	public List<ParameterValue> getParameters() throws DAOException {
		String selectStatement = "select "+ PARAMETER_FIELDS + " from ysf_cc_parameterValue PAR";
		
		return listQuery(selectStatement);
	}

}
